package com.hou.shopping.product.service.impl;

import com.hou.shopping.product.dao.CategoryBrandRelationDao;
import com.hou.shopping.product.service.CategoryBrandRelationService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hou.shopping.common.utils.PageUtils;
import com.hou.shopping.common.utils.Query;
import com.hou.shopping.product.dao.BrandDao;
import com.hou.shopping.product.entity.BrandEntity;
import com.hou.shopping.product.service.BrandService;


@Service("brandService")
public class BrandServiceImpl extends ServiceImpl<BrandDao, BrandEntity> implements BrandService {

    @Autowired
    CategoryBrandRelationService categoryBrandRelationService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
//        1.获取key
        String key = (String) params.get("key");
        if (!StringUtils.isEmpty(key)) {
            QueryWrapper<BrandEntity> queryWrapper = new QueryWrapper<BrandEntity>()
                    .eq("brand_id", key).or().like("name", key);
            IPage<BrandEntity> page = this.page(
                    new Query<BrandEntity>().getPage(params),
                    queryWrapper
            );
            return new PageUtils(page);
        }
        IPage<BrandEntity> page = this.page(
                new Query<BrandEntity>().getPage(params),
                new QueryWrapper<BrandEntity>()
        );
        return new PageUtils(page);
    }

    @Override
    public void updateDetail(BrandEntity brand) {
//        保证冗余字段的数据一致
        this.updateById(brand);
        if (!StringUtils.isEmpty(brand.getName())) {
            //同步更新关联表中的数据
            categoryBrandRelationService.updateBrand(brand.getBrandId(), brand.getName());
//            TODO:更新其他关联
        }
    }

}